package com.huazai.yangge.completable.future;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @author 华仔
 * @date 2024/2/21
 */
public class CompletableFutureAPIDemo {

    //    public static void main(String[] args) throws ExecutionException, InterruptedException {
//
//        // 注意：不加线程池就是守护线程 加了线程池就班车用户线程
//        final ExecutorService executorService = Executors.newFixedThreadPool(5);
//        CompletableFuture.supplyAsync(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("111");
//            return 1;
//        }, executorService).thenApply(integer -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("222");
//            return integer + 2;
//        }).thenApply(integer -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("333");
//            return integer + 3;
//        }).whenComplete((result, throwable) -> {
//            if (throwable == null) {
//                System.out.println("计算结果 = " + result);
//            }
//        }).exceptionally(throwable -> {
//            throwable.printStackTrace();
//            System.out.println("throwable = " + throwable.getMessage());
//            return null;
//        });
//        System.out.println(Thread.currentThread().getName() + "====主线程去忙其他的工作");
//        executorService.shutdown();
//    }
//}
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 注意：不加线程池就是守护线程 加了线程池就班车用户线程
        final ExecutorService executorService = Executors.newFixedThreadPool(5);
        try {
            CompletableFuture.supplyAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("111");
                return 1;
            }, executorService).thenApply(integer -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("222");
                return integer + 2;
            }).thenApply(integer -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("333");
                return integer + 3;
            }).whenComplete((result, throwable) -> {
                if (throwable == null) {
                    System.out.println("计算结果 = " + result);
                }
            }).exceptionally(throwable -> {
                throwable.printStackTrace();
                System.out.println("throwable = " + throwable.getMessage());
                return null;
            });
            System.out.println(Thread.currentThread().getName() + "====主线程去忙其他的工作");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            executorService.shutdown();
        }
    }
}

// thenRun 任务A执行完执行任务B， 并且B不需要A的结果
// thenAccept 任务A执行完执行任务B， B需要A的结果， 但是B无返回值
// thenApply 任务A执行完执行任务B， B需要A的结果 同时任务B有返回值






